package com.gui.windows.components;

import java.util.List;

import javax.swing.JComboBox;

import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.Bindings;
import org.jdesktop.beansbinding.ELProperty;
import org.jdesktop.swingbinding.JComboBoxBinding;
import org.jdesktop.swingbinding.SwingBindings;

/**
 * Gives the functionality of a JComboBox and auto binds to a String List 
 * passed as parameter to the constructor.
 * */
public class ComboBoxAutoBinded extends JComboBox {

	//**************************************************************************************************
	//	Constructor.
	//**************************************************************************************************
	
	public ComboBoxAutoBinded (List <String> list) {
		this.list = list;
		bindingGroup = new BindingGroup();
		bindComboBoxWithList(this, this.list);
	}
	
	//**************************************************************************************************
	//	Attributes.
	//**************************************************************************************************
	
	private final byte OUT_OF_RANGE = -1;
	
	private static final long serialVersionUID = 1L;
	private BindingGroup bindingGroup;
	private List <String> list;
	private int currentSelected = OUT_OF_RANGE;
	private int previousSelected = OUT_OF_RANGE;
	
	//**************************************************************************************************
	//	Public Methods.
	//**************************************************************************************************
	
	/**
	 * Sets the selected item in the combo box.</br>
	 * It also refreshes the values of the previous and current selected items. 
	 * @param idx The index to be set as current.
	 * */
	public void setSelected (int idx) {
		super.setSelectedIndex(idx);
		previousSelected = currentSelected;
		currentSelected = idx;
	}
	
	public void update () {
		previousSelected = currentSelected; 
		currentSelected = super.getSelectedIndex();
	}
	
	public boolean changedIndex () {
		return currentSelected != previousSelected;
	}
	
	//**************************************************************************************************
	//	Private Methods.
	//**************************************************************************************************
	
	/**
	 * Makes the dynamic binding between a graphic component JComboBox and a <code> String </code> List component.
	 * @param cb {@link JComboBox}
	 * @param data {@link List}
	 * */
	private void bindComboBoxWithList(JComboBox cb, List <String> data) {
		JComboBoxBinding <String, List <String>, JComboBox> comboBoxBinding = SwingBindings.createJComboBoxBinding(AutoBinding.UpdateStrategy.READ, data,cb);
		bindingGroup.addBinding(comboBoxBinding);
		AutoBinding<Object, Object, Object, Object> binding = Bindings.createAutoBinding(AutoBinding.UpdateStrategy.READ, this, ELProperty.create("${currentRecord.countryId}"), cb,BeanProperty.create("selectedItem"));
		bindingGroup.addBinding(binding);
		bindingGroup.bind();
	}

	//**************************************************************************************************
	//	Getters & Setters.
	//**************************************************************************************************
	
	public int getPreviousSelected() {
		return previousSelected;
	}

	public void setPreviousSelected(int previousSelected) {
		this.previousSelected = previousSelected;
	}

	public void setCurrentSelected(int currentSelected) {
		this.currentSelected = currentSelected;
	}

	public int getCurrentSelected() {
		return currentSelected;
	}
}
